﻿using System;
using System.Collections.Generic;
using System.Text;
using Cashier_DAO.db;
using Cashier_Model;
using Cashier_DAO;
using System.Data;
using System.Data.Common;

namespace Cashier_Service
{
    public class CategoryService
    {
        /// <summary>
        /// 查询商品分类信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public Category GetCategory(long id)
        {
            Category category = null;
            try
            {
                using (DbConnection conn = ConnectionFactory.GetDbConnection())
                {
                    conn.Open();
                    category = new Category();
                    category.Id = id;
                    category = new CategoryDAO().GetCategory(conn, category);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return category;
        }

        /// <summary>
        /// 查询商品分类列表
        /// </summary>
        /// <param name="pageindex"></param>
        /// <param name="pagesize"></param>
        /// <param name="parentid"></param>
        /// <returns></returns>
        public DataTable GetCategoryTable(int pageindex, int pagesize, long parentid)
        {
            DataTable dt = null;
            try
            {
                using (DbConnection conn = ConnectionFactory.GetDbConnection())
                {
                    conn.Open();
                    Category category = new Category();
                    category.Pageindex = pageindex;
                    category.Pagesize = pagesize;
                    category.Parentid = parentid;
                    dt = new CategoryDAO().GetCategoryTable(conn, category);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return dt;
        }

        /// <summary>
        /// 查询商品分类记录数
        /// </summary>
        /// <param name="parentid"></param>
        /// <returns></returns>
        public long GetCategoryRecord(long parentid)
        {
            long record = 0;
            try
            {
                using (DbConnection conn = ConnectionFactory.GetDbConnection())
                {
                    conn.Open();
                    Category category = new Category();
                    category.Parentid = parentid;
                    record = new CategoryDAO().GetCategoryRecord(conn, category);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return record;
        }

        /// <summary>
        /// 递归查询所有分类
        /// </summary>
        /// <returns></returns>
        public DataTable GetRecursionCategoryTable()
        {
            DataTable dt = null;
            try
            {
                using (DbConnection conn = ConnectionFactory.GetDbConnection())
                {
                    conn.Open();
                    dt = new DataTable("category");
                    dt.Columns.Add("id");
                    dt.Columns.Add("name");
                    dt.Columns.Add("description");
                    dt.Columns.Add("parent_id");
                    dt.Columns.Add("orderno");
                    dt.Columns.Add("parentname");
                    this.GetRecursionTable(conn, dt, 0, 1);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return dt;
        }

        /// <summary>
        /// 递归查询
        /// </summary>
        /// <param name="conn"></param>
        /// <param name="dt"></param>
        /// <param name="parentid"></param>
        /// <param name="depth"></param>
        private void GetRecursionTable(DbConnection conn, DataTable dt, long parentid, int depth)
        {
            try
            {
                Category category = new Category();
                category.Parentid = parentid;
                DataTable tempdt = new CategoryDAO().GetCategoryTable(conn, category);
                if (tempdt != null)
                {
                    StringBuilder sb = new StringBuilder();
                    for (int i = 1; i < depth; i++)
                    {
                        sb.Append("  ");
                    }

                    foreach (DataRow lopRow in tempdt.Rows)
                    {
                        DataRow newRow = dt.NewRow();
                        newRow["id"] = lopRow["id"];
                        newRow["name"] = sb.Append("-").ToString() + lopRow["name"];
                        newRow["description"] = lopRow["description"];
                        newRow["parent_id"] = lopRow["parent_id"];
                        newRow["orderno"] = lopRow["orderno"];
                        newRow["parentname"] = lopRow["parentname"];
                        dt.Rows.Add(newRow);
                        GetRecursionTable(conn, dt, (long)lopRow["id"], depth + 1);
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 插入商品分类信息
        /// </summary>
        /// <param name="category"></param>
        /// <returns></returns>
        public long InsertCategory(Category category)
        {
            long id = 0;
            try
            {
                using (DbConnection conn = ConnectionFactory.GetDbConnection())
                {
                    conn.Open();
                    id = new CategoryDAO().InsertCategory(conn, category);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return id;
        }

        /// <summary>
        /// 新增商品分类
        /// </summary>
        /// <param name="name"></param>
        /// <param name="parentid"></param>
        /// <param name="orderno"></param>
        /// <returns></returns>
        public long InsertCategory(String name, long parentid, int orderno)
        {
            long id = 0;
            try
            {
                using (DbConnection conn = ConnectionFactory.GetDbConnection())
                {
                    conn.Open();
                    Category category = new Category();
                    category.Name = name;
                    category.Parentid = parentid;
                    category.Orderno = orderno;
                    id = new CategoryDAO().InsertCategory(conn, category);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return id;
        }

        /// <summary>
        /// 更新商品分类
        /// </summary>
        /// <param name="category"></param>
        /// <returns></returns>
        public int UpdateCategory(Category category)
        {
            int flag = 0;
            try
            {
                using (DbConnection conn = ConnectionFactory.GetDbConnection())
                {
                    conn.Open();
                    flag = new CategoryDAO().UpdateCategory(conn, category);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return flag;
        }

        /// <summary>
        /// 更新商品分类
        /// </summary>
        /// <param name="id"></param>
        /// <param name="name"></param>
        /// <param name="parentid"></param>
        /// <param name="orderno"></param>
        /// <returns></returns>
        public int UpdateCategory(long id, String name, long parentid, int orderno)
        {
            int flag = 0;
            try
            {
                using (DbConnection conn = ConnectionFactory.GetDbConnection())
                {
                    conn.Open();
                    Category category = new Category();
                    category.Id = id;
                    category.Name = name;
                    category.Parentid = parentid;
                    if (orderno > 0)
                        category.Orderno = orderno;
                    flag = new CategoryDAO().UpdateCategory(conn, category);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return flag;
        }

        /// <summary>
        /// 删除商品分类
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public int DeleteCategory(String ids)
        {
            int count = 0;
            try
            {
                using (DbConnection conn = ConnectionFactory.GetDbConnection())
                {
                    if (!String.IsNullOrEmpty(ids))
                    {
                        conn.Open();
                        String[] arr = ids.Split(new Char[] { ',' });
                        CategoryDAO categoryDAO = new CategoryDAO();
                        Category category = new Category();
                        foreach (String lopid in arr)
                        {
                            category.Id = Int64.Parse(lopid);
                            count += categoryDAO.DeleteCategory(conn, category);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return count;
        }

        /// <summary>
        /// 判断是否存在同名的分类名称
        /// </summary>
        /// <param name="id"></param>
        /// <param name="parentid"></param>
        /// <param name="name"></param>
        /// <returns></returns>
        public bool ExistCategoryName(long id, long parentid, String name)
        {
            bool exist = true;
            try
            {
                using (DbConnection conn = ConnectionFactory.GetDbConnection())
                {
                    conn.Open();
                    Category category = new Category();
                    category.Id = id;
                    category.Parentid = parentid;
                    category.Name = name;
                    exist = new CategoryDAO().ExistCategoryName(conn, category);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return exist;
        }
    }
}
